import Request from '@/utils/luch-request/index.js'
const http = new Request()
http.setConfig(config => {
  /* config 为默认全局配置*/
  config.timeout = 100000 /* 根域名 */
  return config
})
http.interceptors.request.use((config, cancel) => {
  uni.showLoading({
    title: '加载中...',
    mask: true
  })
  /* cancel 为函数，如果调用会取消本次请求。需要注意：调用cancel,本次请求的catch仍会执行。必须return config */
  config.header = {
    ...config.header
    // Authorization: `${getApp().globalData.token}` // 演示拦截器header加参
  }
  return config
})
// 自定义验证器 详见 https://quanzhan.co/luch-request/guide/#validatestatus
http.validateStatus = statusCode => {
  return statusCode && statusCode >= 200 && statusCode < 300
}
http.interceptors.response.use(
  response => {
    uni.hideLoading()
    // 成功
    if (response.data && response.data.code == 0) {
      if (response.data.body) {
        return response.data.body
      }
      return response.data
    } else if (response.data && response.data.code == 210) {
      return Promise.reject(response.data)
    } else if (response.statusCode == 204) {
      return '操作成功'
    } else {
      return Promise.reject(response.data.msg)
    }
  },
  response => {
    uni.hideLoading()
    // 失败
    console.log('response-------->', response)
    if (response && response.data) {
      return response.data
    } else if (response && response.errMsg) {
      uni.showToast({
        title: response.errMsg,
        icon: 'none',
        mask: true
      })
      return response
    }
    return response
  }
)
/* const resetData = (data, url) => {
  console.log(`${url}------
  ${data ? JSON.stringify(data) : ''}`)
  return {
    id: new Date().getTime(),
    sign: 'string',
    timestamp: new Date().getTime(),
    body: data.body ? data.body : {}
  }
} */
export const POST = url => {
  return body => http.post(url, body)
}
export const PUT = url => {
  return body => http.put(url, body)
}
export const GET = url => {
  return body => http.get(url, body)
}
export const DELETE = url => {
  return body => http.delete(url, body)
}
